package com.common.queueloader.items
{
	import com.common.queueloader.AbstractItem;
	import com.common.queueloader.ILoadable;
	import com.common.queueloader.QueueVo;
	
	import flash.display.Loader;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	
	public class SwfItem extends AbstractItem implements ILoadable {

		protected var _smoothing : Boolean = false;
		
		protected var _center : Boolean = false;
		
		public function SwfItem(queueVo:QueueVo,path : URLRequest, fileType:int) 
		{
			super(queueVo, path, fileType);
		}

		override public function load() : void 
		{
			var applicationDomain :ApplicationDomain = queueVo.applicationDomain;
			if(applicationDomain == null)
			{
				applicationDomain = ApplicationDomain.currentDomain;
			}
			var context:LoaderContext = new LoaderContext(false,applicationDomain)
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS , onProgress);
			_loader.load( path , context );
		}

		override public function dispose() : void 
		{
			_progress = 0;
			_bytesLoaded = 0;
			_bytesTotal = 0;
			_queueVo = null;
			_path = null;
			_isLoading = false;
			_fileType = 0;
			_loader = null;
			_httpStatusFunction = null;
			_errorFunction = null;
			_completeFunction = null;
			_progressFunction = null;
		}
		
		override public function close():void
		{
			if(_loader)
			{
				Loader(_loader).unloadAndStop(true);
			}
		}

		
		override public function onComplete(evt:Event):void 
		{
			_content = evt.target.content;
			_completeFunction(evt);
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS , onProgress);
			this.dispose();
        }
        override public function onProgress(evt:ProgressEvent):void
        {
        	if(_progressFunction==null)return;
        	_bytesLoaded = evt.bytesLoaded;
        	_bytesTotal = evt.bytesTotal;
        	_progress = Math.floor(evt.bytesLoaded / evt.bytesTotal * 100 );
        	_progressFunction(evt.clone());
        }
        override public function onHttpStatus(evt:HTTPStatusEvent):void
        {
        	
        }
        override public function onError(evt:ErrorEvent):void
        {
        	
        }
	}
}
